package com.lgx.mall.core.dao.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.lgx.common.basic.dao.GeneralDao;
import com.lgx.common.dao.IImageDao;
import com.lgx.common.exception.DataException;
import com.lgx.common.web.Pagination;
import com.lgx.mall.core.dao.IBrandDao;
import com.lgx.mall.core.entity.Brand;

@Component("brandDao")
public class BrandDao extends GeneralDao  implements IBrandDao {
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	private IImageDao imageDao;

	public IImageDao getImageDao() {
		return imageDao;
	}

	public void setImageDao(IImageDao imageDao) {
		this.imageDao = imageDao;
	}
	
	@Transactional
	public Brand createBrand(Brand brand) throws DataException {
		this.getHibernateTemplate().save(brand);
		log.info("[创建商品品牌]:"+brand.getId());
		return brand;
	}

	@Transactional
	public Brand updateBrand(Brand brand) throws DataException {
		this.getHibernateTemplate().update(brand);
		log.info("[更新商品品牌]:"+brand.getId());
		return brand;
	}

	@Transactional
	public void deleteBrand(Brand brand) throws DataException {
		log.info("[删除商品品牌]:"+brand.getId());
		//
		if(!StringUtils.isEmpty(brand.getImageUrl()))this.imageDao.deleteImageByFilePath(brand.getImageUrl());
		this.getHibernateTemplate().delete(brand);
	}

	public void deleteBrand(long id) throws DataException {
		this.deleteBrand(this.findBrandById(id));
		log.info("[删除商品品牌]:"+id);
	}

	
	public Brand findBrandById(long id) {
		return this.getHibernateTemplate().get(Brand.class, id);
	}

	@SuppressWarnings("unchecked")
	public List<Brand> findBrandList(String sql) {	
		return this.getHibernateTemplate().find(sql);
	}
	
	@SuppressWarnings("unchecked")
	public List<Brand> findBrandList(){
		String hql = "from Brand b order by id asc";
		return this.getHibernateTemplate().find(hql);
	}

	@SuppressWarnings("unchecked")
	public List<Brand> findBrandList(String sql, Object[] params, int pageSize,
			int pageNum) {
		return this.getList(sql, params, pageSize, pageNum);
	}


	public Pagination findBrandPagination(String sql, Object[] params,
			int pageSize, int pageNum) {	
		return this.getListPagination(sql, params, pageSize, pageNum);
	}

	
	public Pagination findBrandPagination(Object[] params, int pageSize,
			int pageNum) {	
		String sql = "from Brand b order by id asc";
		return this.getListPagination(sql, params, pageSize, pageNum);
	}

	public void deleteBrands(String[] ids) throws DataException {
		for(String id : ids)
		{
			this.deleteBrand(Long.parseLong(id));
		}
	}

	/**
	 * 根据品牌名字模糊查询
	 */
	@SuppressWarnings("unchecked")
	public List<Brand> findBrandByName(String name) {
		String sql = "from Brand b where b.isdelete='0'";
		if(name!=null&&!("").equals(name)&&!("null").equals(name)) {
			sql += " and b.name like '%"+name+"%'";
		}
		sql += " order by b.id asc";
		return this.getHibernateTemplate().find(sql);
	}
	
}
